<?php

class BShopOrderCount extends BaseModel
{
    public $res;
    public $expire;

    public function getSource()
    {
        return "b_shop_order_count";
    }

    public function get_list($data)
    {
        if (!isset($data['page']) || empty($data['page'])) {
            $data['page'] = 1;
        }

        $page       = intval($data['page']);
        $size       = 10;
        $conditions = "1 = 1";
        if (isset($data['order_status']) && $data['order_status'] > 0) {
            $conditions .= " and order_status = {$data['order_status']}";
        }

        if(isset($data['phone']) && !empty($data['phone'])){
            $user = new User();
            $info = $user->findFirstByPhone($data['phone']);
            if($info){
                $conditions .= " and user_id = '{$info->id}'";
            }
        }
        if (isset($data['order_no']) && !empty($data['order_no'])) {
            $conditions .= " and order_no = '{$data['order_no']}'";
        }
        $data['total'] = $this::count($conditions);
        $data['list'] = $this->find(array(
            "conditions" => $conditions,
            'limit'      => $size,
            'order'      => 'id desc',
            "offset"     => ($page - 1) * $size,
        ))->toarray();
        return $data;
    }

    public function detailed($no){
        $list    = $this->findFirstById($no);
        if ($list) {
            $zOrder              = new BShopOrder();
            $ids                 = explode(',', $list->order_id);
            $list = $list->toarray();
            $addressDb = new BShopAddress();
            $addInfo = $addressDb->findFirstById($list['address_id']);
            $list['address'] = (object)null;
            if($addInfo){
                $list['address'] = $addInfo->toarray();
            }
            $list['order'] = [];
//                $list[$key]['order_status'] = CommonController::ORDER_STATUS[$val['order_status']];
            if (!empty($ids)) {
                foreach ($ids as $idVal) {
                    $zInfo                 = $zOrder->getOrder($idVal);
                    $list['order'][] = $zInfo;
                }
            }
            return $list;
        } else {
            return false;
        }
    }

    /**
     * 确认收货
     * @param $orderNo
     */
    public function receivingGoods($orderNo)
    {
        $where = "user_id = {$orderNo['user_id']} and order_no =  '{$orderNo['order_no']}'";
        $info = $this->findFirst($where);
        if ($info) {
            if ($info->order_status != 4) {
                return '此订单状态不能收货';
            }
            $orderArr = explode(',',$info->order_id);

            foreach ($orderArr as $val){
                $orderDb = new BShopOrder();
                $zInfo = $orderDb->findFirstByOrderNo($val);
                $zInfo->order_status = 6;
                $zInfo->updated_at = date('Y-m-d H:i:s');
                if(!$zInfo->save()){
                    return '子订单修改失败!';
                }
            }

            $info->order_status = 6;
            $info->updated_at   = date('Y-m-d H:i:s');
            if (!$info->save()) {
                return '订单状态修改失败!';
            }

            if ($info->growth_value > 0) {
                $userDb                 = new User();
                $userInfo               = $userDb->find_by_id($info->user_id);
                $userInfo->growth_value = $userInfo->growth_value + $info->growth_value;
                $is_up                  = $this->level_exp($userInfo->growth_value);
                if ($is_up > $userInfo->level_id) {
                    $userInfo->level_id           = $is_up;
                    $userInfo->level_promotion_at = date('Y-m-d H:i:s');
                }

                if (!$userInfo->save()) {
                    return '成长值添加失败!';
                }
            }
            return true;
        } else {
            return '订单不存在';
        }
    }

    public function level_exp($gv)
    {
        $level = 1;
        if ($gv >= 5000) {
            $level = 2;
        } elseif ($gv >= 15000) {
            $level = 3;
        }
        return $level;
    }
}
